import DvaDynamic from 'dva/dynamic';
import React,{Component} from 'react';

function dynamic(options){
    return class Views extends Component{

        state = {
            Com:null
        }

        componentDidMount(){
            //1.过滤需要做守卫的路由
            let {route:{path,view_id}} = this.props;

            if(/^\/main\//.test(path)){
                //需要路由守卫
                //2.如果没有路由权限，展示您没有权限组件，有权限，展示对应的组件
                let userViewsData = JSON.parse(localStorage.getItem('userViewsData'));

                let target = userViewsData.find(item => item.view_id === view_id);

                console.log(target);

                if(target){
                    //有权限
                    this.setState({
                        Com:DvaDynamic(options)
                    })
                }else{
                    //没有权限
                    this.setState({
                        Com:()=><div>您没有视图访问权限</div>
                    })
                }

            }else{
                //不需要
                this.setState({
                    Com:DvaDynamic(options)
                })
            }
        }

        render(){
            let {Com} = this.state;
            return (
                Com && <Com/>
            )
        }
    }
}

export default dynamic
